package com.dqp.cslggroup.Features;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Group;

import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.BaseActivity;
import com.dqp.cslggroup.Util.UiUtils;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.Objects;

public class CET extends BaseActivity {
    private String chsi_CET = "https://www.chsi.com.cn/cet/index.jsp";
    private String neea_CET = "http://cet.neea.edu.cn/cet/";
    private String back_CET = "http://cet-bm.neea.cn/Home/QueryTestTicket";
    private WebView webView;
    private FloatingActionButton mAdd;
    private FloatingActionButton mLike;
    private FloatingActionButton mLose;
    private FloatingActionButton mFind;
    private Group likeGroup;
    private Group FindGroup;
    private Group LoseGroup;
    private AnimatorSet animatorSet;    // 动画集合，用来控制动画的有序播放
    private int radius;    // 圆的半径
    private int width;    // FloatingActionButton宽度和高度，宽高一样
    private ProgressBar pb;

    @SuppressLint("SetJavaScriptEnabled")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cet);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        webSetting();
        initWidget();
        initListener();
    }


    @SuppressLint("SetJavaScriptEnabled")
    private void webSetting() {
        webView = findViewById(R.id.cet_web);
        webView.clearCache(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        webView.getSettings().setJavaScriptEnabled(true);    //支持javascript
        webView.getSettings().setSupportZoom(true);        // 设置可以支持缩放
        webView.getSettings().setUseWideViewPort(true);        //扩大比例的缩放
        webView.getSettings().setBuiltInZoomControls(true); // 设置出现缩放工具
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//自适应屏幕
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                view.loadUrl(url);
                return true;
            }
        });
        pb = findViewById(R.id.cet_pb);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO 自动生成的方法存根
                if (newProgress == 100) {
                    pb.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    pb.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    pb.setProgress(newProgress);//设置进度值
                }

            }
        });
        webView.loadUrl(neea_CET);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAdd.post(() -> width = mAdd.getMeasuredWidth());        // 动态获取FloatingActionButton的宽
        radius = UiUtils.dp2px(this, 80);    // 在xml文件里设置的半径
    }

    private void initWidget() {
        mAdd = findViewById(R.id.fab_add);
        mLike = findViewById(R.id.fab_like);
        mLose = findViewById(R.id.fab_top);
        mFind = findViewById(R.id.fab_write);
        likeGroup = findViewById(R.id.gp_like);
        FindGroup = findViewById(R.id.gp_write);
        LoseGroup = findViewById(R.id.gp_top);
        mFind.setOnClickListener(view -> webView.loadUrl(chsi_CET));
        mLose.setOnClickListener(view -> webView.loadUrl(neea_CET));
        mLike.setOnClickListener(view -> webView.loadUrl(back_CET));
        setViewVisible(false);        // 将三个弹出的FloatingActionButton隐藏
    }

    private void setViewVisible(boolean isShow) {
        likeGroup.setVisibility(isShow ? View.VISIBLE : View.GONE);
        FindGroup.setVisibility(isShow ? View.VISIBLE : View.GONE);
        LoseGroup.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    private void initListener() {
        mAdd.setOnClickListener(v -> {
            // 播放动画的时候不可以点击
            if (animatorSet != null && animatorSet.isRunning())
                return;
            // 判断播放显示还是隐藏动画
            if (likeGroup.getVisibility() != View.VISIBLE) {
                animatorSet = new AnimatorSet();
                ValueAnimator likeAnimator = getValueAnimator(mLike, false, likeGroup, 0);
                ValueAnimator writeAnimator = getValueAnimator(mFind, false, FindGroup, 45);
                ValueAnimator topAnimator = getValueAnimator(mLose, false, LoseGroup, 90);
                animatorSet.playSequentially(likeAnimator, writeAnimator, topAnimator);
                animatorSet.start();
            } else {
                animatorSet = new AnimatorSet();
                ValueAnimator likeAnimator = getValueAnimator(mLike, true, likeGroup, 0);
                ValueAnimator writeAnimator = getValueAnimator(mFind, true, FindGroup, 45);
                ValueAnimator topAnimator = getValueAnimator(mLose, true, LoseGroup, 90);
                animatorSet.playSequentially(topAnimator, writeAnimator, likeAnimator);
                animatorSet.start();
            }

        });
    }

    private ValueAnimator getValueAnimator(final FloatingActionButton button,
                                           final boolean reverse, final Group group, final int angle) {
        ValueAnimator animator;
        if (reverse)
            animator = ValueAnimator.ofFloat(1, 0);
        else
            animator = ValueAnimator.ofFloat(0, 1);
        animator.addUpdateListener(animation -> {
            float v = (float) animation.getAnimatedValue();
            ConstraintLayout.LayoutParams params
                    = (ConstraintLayout.LayoutParams) button.getLayoutParams();
            params.circleRadius = (int) (radius * v);
            //params.circleAngle = 270f + angle * v;
            params.width = (int) (width * v);
            params.height = (int) (width * v);
            button.setLayoutParams(params);
        });
        animator.addListener(new SimpleAnimation() {
            @Override
            public void onAnimationStart(Animator animation) {
                group.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (group == likeGroup && reverse) {
                    setViewVisible(false);
                }
            }
        });
        animator.setDuration(200);
        animator.setInterpolator(new DecelerateInterpolator());
        return animator;
    }

    abstract class SimpleAnimation implements Animator.AnimatorListener {
        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}